package com.sbpm.crmbpm.client.panels;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.ListBox;
import com.sbpm.crmbpm.client.forms.companies.CompanyForm;
import com.sbpm.crmbpm.client.grid.CompanyGrid;
import com.sbpm.crmbpm.client.handlers.OnFinishEvent;
import com.sbpm.crmbpm.client.handlers.SearchClientClickHandler;
import com.sbpm.crmbpm.client.util.CompanySearchUtils;
import com.sbpm.crmbpm.client.util.FieldUtils;
import com.sbpm.crmbpm.shared.data.client.Segment;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.CenterLayoutContainer;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FieldSet;
import com.sencha.gxt.widget.core.client.form.TextField;

/**
 * todo change layout containers to boxlayoutcontainers
 */
public class CompanySearchPanel extends FlowLayoutContainer
{



    private TextField clientName;
    private ListBox segment;
    private TextField inn;
    private TextField ogrn;
    private TextField kpp;
    private CompanyGrid companyGrid;

    private CompanyForm companyForm;

    private CompanySearchUtils companySearchUtils;

    public CompanySearchPanel()
    {
//        setWidth( 600 );
        FlowLayoutContainer mainContainer = new FlowLayoutContainer();

        add( mainContainer, new VerticalLayoutContainer.VerticalLayoutData( -1, -1, new Margins( 10, 10, 10, 10 ) ) );

        FieldSet fieldSet = new FieldSet();
        fieldSet.setHeadingText( "Поиск клиента" );
        fieldSet.setCollapsible( false );
        fieldSet.setWidth( 600 );
//        fieldSet.setWidth( "100%" );

        mainContainer.add( fieldSet, new VerticalLayoutContainer.VerticalLayoutData( -1, -1, new Margins( 0, 0, 10, 0 ) ) );

        VerticalLayoutContainer fieldSetContainer = new VerticalLayoutContainer();
//        fieldSetContainer.setWidth( "100%" );
        fieldSetContainer.setHeight( 200 );
        fieldSet.add( fieldSetContainer );

        HorizontalLayoutContainer nameHorizontalContainer = new HorizontalLayoutContainer();
        nameHorizontalContainer.setHeight( 30 );
        fieldSetContainer.add( nameHorizontalContainer );

        clientName = new TextField();
//        clientName.setAllowBlank( false );
        clientName.setEmptyText( "Введите название компании..." );
        clientName.setWidth( 345 );

        nameHorizontalContainer.add( new FieldLabel( clientName, "Название компании" ) );

        companyForm = new CompanyForm();

        TextButton newButton = new TextButton( "Новая компания" );
        newButton.addSelectHandler( new SelectEvent.SelectHandler()
        {
            @Override
            public void onSelect( SelectEvent event )
            {
                companyForm.show( null );
            }
        } );

        nameHorizontalContainer.add( newButton, new HorizontalLayoutContainer.HorizontalLayoutData( -1, -1, new Margins( 0, 0, 0, 10 ) ) );

        HorizontalLayoutContainer horizontalLayoutContainer = new HorizontalLayoutContainer();
        horizontalLayoutContainer.setHeight( 140 );


        fieldSetContainer.add( horizontalLayoutContainer );

        VerticalLayoutContainer group1Container = new VerticalLayoutContainer();

        inn = new TextField();
        inn.setEmptyText( "Введите ИНН компании..." );
        group1Container.add( new FieldLabel( inn, "ИНН" ) );

        ogrn = new TextField();
        ogrn.setEmptyText( "Введите ОГРН компании..." );
        group1Container.add( new FieldLabel( ogrn, "ОГРН" ) );

        kpp = new TextField();
        kpp.setEmptyText( "Введите КПП компании..." );
        group1Container.add( new FieldLabel( kpp, "КПП" ) );


        horizontalLayoutContainer.add( group1Container, new HorizontalLayoutContainer.HorizontalLayoutData( -1, -1, new Margins( 0, 10, 0, 0 ) ) );


        VerticalLayoutContainer group2Container = new VerticalLayoutContainer();

        segment = new ListBox();
        segment.addItem( "", "" );
        segment.addItem( Segment.HIGH.getLabel(), Segment.HIGH.name() );
        segment.addItem( Segment.MEDIUM.getLabel(), Segment.MEDIUM.name() );
        segment.addItem( Segment.LOW.getLabel(), Segment.LOW.name() );

        group2Container.add( new FieldLabel( segment, "Сегмент" ) );

        horizontalLayoutContainer.add( group2Container );

        CenterLayoutContainer centerLayoutContainer = new CenterLayoutContainer();
//        centerLayoutContainer.setHeight( 10 );
        fieldSetContainer.add( centerLayoutContainer );

        TextButton searchButton = new TextButton( "Поиск" );
        centerLayoutContainer.add( searchButton );

        companyGrid = new CompanyGrid( companyForm );

        mainContainer.add( companyGrid, new VerticalLayoutContainer.VerticalLayoutData( -1, -1, new Margins( 0, 0, 0, 0 ) ) );

        companySearchUtils = new CompanySearchUtils( this, companyGrid );

        searchButton.addSelectHandler( new SearchClientClickHandler( companySearchUtils ) );

        companyForm.setOnFinishEvent( new OnFinishEvent()
        {
            @Override
            public void finish()
            {
                companySearchUtils.search();
            }
        } );

    }


    public String getClientName()
    {
        return FieldUtils.getStringOrNull( clientName );
    }

    public Segment getSegment()
    {
        return segment.getValue( segment.getSelectedIndex() ).length() > 0 ? Segment.valueOf( segment.getValue( segment.getSelectedIndex() ) ) : null;
    }

    public String getInn()
    {
        return FieldUtils.getStringOrNull( inn );
    }

    public String getOgrn()
    {
        return FieldUtils.getStringOrNull( ogrn );
    }

    public String getKpp()
    {
        return FieldUtils.getStringOrNull( kpp );
    }
}
